#include "InnerMTypes.h"
#include "MException.h"
#include "MTestUtil.h"
#include "MList.h"
#include "MEnviroment.h"
#include <list>

using namespace Marionette;
using namespace std;
MString testStructXml="\
<struct name=\"TargetType\">\r\n\
<DWORD name=\"ID\" offset=\"0x0000\"/>\r\n\
<DWORD name=\"AttackSid\" offset=\"0x0004\"/>\r\n\
<DWORD name=\"Cur_X\" offset=\"0x0008\"/>\r\n\
<DWORD name=\"Cur_Y\" offset=\"0x000c\"/>\r\n\
<DWORD name=\"StateFlag\" offset=\"0x0010\"/>\r\n\
</struct>\r\n";

// MString MListXml = "\
// <MList begin=\"0x0004\" countOffset=\"0x0008\">\r\n\
// <Node next=0x0000 prev=\"0x0004\" valueOffset=\"0x0008\" valueType=\"TargetType\">\r\n\
// </MList>\r\n";
MString MListXml = "<MList name=\"TargetList\" valueType=\"TargetType\"/>\r\n";

TEST(MListTest,DefaultValueType_DWORD)
{
	list<int> targetList;
	targetList.push_back(12);
	targetList.push_back(13);
	targetList.push_back(12);
	targetList.push_back(0);
	targetList.push_back(-112);

	MEnviroment env;
 	MList targetMListType("IntList",&env);
 
	MValue ml = targetMListType.CreateValue((DWORD)&targetList);	    

	ASSERT_EQ(targetList.size(),ml.Count());
	list<int>::iterator iter = targetList.begin();
	
	for(MValue miter = ml.Begin();miter != ml.End();miter=miter.Next())
	{
		ASSERT_EQ((*iter),(int)(miter.Value().ToDWORD()));
		iter++;
	}

}
TEST(MListTest,StructTypeList)
{
	struct targetStruct{
		DWORD ID;
		DWORD AtackSid;
		DWORD x,y;
		DWORD StateFlag;
	};
	list<targetStruct> targetList;
	targetStruct temp;
	temp.ID = 1;
	temp.x = 20;
	temp.y = 20;
	targetList.push_back(temp);

	MEnviroment env;
	MStruct* pStruct = (MStruct*)env.NewStruct("TargetType");
	pStruct->Import(testStructXml);

	MList* pList = (MList*) env.NewList("TargetList");
	pList->Import(MListXml);


	MValue ml = env.CreateValue("TargetList","ml",(DWORD)&targetList);
	list<targetStruct>::iterator iter = targetList.begin();

	for(MValue miter = ml.Begin();miter != ml.End();miter=miter.Next())
	{
		MValue targetValue = miter.Value();
		ASSERT_EQ(iter->ID,targetValue["ID"].ToDWORD());
		ASSERT_EQ(iter->x,targetValue["Cur_X"].ToDWORD());
		ASSERT_EQ(iter->y,targetValue["Cur_Y"].ToDWORD());
		iter++;
	}
}